package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_gc.domain.bo.GcjxljjtbgBsInfoBo;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntKwlqdBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKwlqdBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntKwlqdbgBsInfoBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntKwlqdbgBsInfoVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntKwlqdbgBsInfo;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKwlqdbgBsInfoMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntKwlqdbgBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥混凝土抗弯拉强度试验检测报告Service业务层处理
 *
 * @author zxs
 * @date 2024-08-12
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntKwlqdbgBsInfoServiceImpl implements IReportSnhntKwlqdbgBsInfoService {

    private final ReportSnhntKwlqdbgBsInfoMapper baseMapper;
    private final ReportSnhntKwlqdBaDetailMapper detailMapper;

    /**
     * 查询水泥混凝土抗弯拉强度试验检测报告
     */
    @Override
    @FIllBasicInfo
    public ReportSnhntKwlqdbgBsInfoVo queryById(Long excelId){
        ReportSnhntKwlqdbgBsInfoVo vo = baseMapper.selectVoById(excelId);
        List<ReportSnhntKwlqdBaDetail> detailList = detailMapper.selectByBgExcelId(excelId);
        vo.setDetailList(detailList);
        return vo;
    }

    /**
     * 查询水泥混凝土抗弯拉强度试验检测报告列表
     */
    @Override
    public TableDataInfo<ReportSnhntKwlqdbgBsInfoVo> queryPageList(ReportSnhntKwlqdbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntKwlqdbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntKwlqdbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥混凝土抗弯拉强度试验检测报告列表
     */
    @Override
    public List<ReportSnhntKwlqdbgBsInfoVo> queryList(ReportSnhntKwlqdbgBsInfoBo bo) {
        LambdaQueryWrapper<ReportSnhntKwlqdbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntKwlqdbgBsInfo> buildQueryWrapper(ReportSnhntKwlqdbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntKwlqdbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSnhntKwlqdbgBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSnhntKwlqdbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSnhntKwlqdbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), ReportSnhntKwlqdbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), ReportSnhntKwlqdbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSnhntKwlqdbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSnhntKwlqdbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSnhntKwlqdbgBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSnhntKwlqdbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSnhntKwlqdbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSnhntKwlqdbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitbh()), ReportSnhntKwlqdbgBsInfo::getWeitbh, bo.getWeitbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJianclb()), ReportSnhntKwlqdbgBsInfo::getJianclb, bo.getJianclb());
        lqw.eq(StringUtils.isNotBlank(bo.getHunntzl()), ReportSnhntKwlqdbgBsInfo::getHunntzl, bo.getHunntzl());
        lqw.eq(bo.getShiyrq() != null, ReportSnhntKwlqdbgBsInfo::getShiyrq, bo.getShiyrq());
        lqw.eq(StringUtils.isNotBlank(bo.getKangsdj()), ReportSnhntKwlqdbgBsInfo::getKangsdj, bo.getKangsdj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancxm()), ReportSnhntKwlqdbgBsInfo::getJiancxm, bo.getJiancxm());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszb()), ReportSnhntKwlqdbgBsInfo::getJiszb, bo.getJiszb());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportSnhntKwlqdbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportSnhntKwlqdbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSnhntKwlqdbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSnhntKwlqdbgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSnhntKwlqdbgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSnhntKwlqdbgBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSnhntKwlqdbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjg()), ReportSnhntKwlqdbgBsInfo::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), ReportSnhntKwlqdbgBsInfo::getJiegpd, bo.getJiegpd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportSnhntKwlqdbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportSnhntKwlqdbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportSnhntKwlqdbgBsInfo::getPandyjIds, bo.getPandyjIds());
        return lqw;
    }

    /**
     * 新增水泥混凝土抗弯拉强度试验检测报告
     */
    @Override
    public Boolean insertByBo(ReportSnhntKwlqdbgBsInfoBo bo) {
        ReportSnhntKwlqdbgBsInfo add = BeanUtil.toBean(bo, ReportSnhntKwlqdbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改水泥混凝土抗弯拉强度试验检测报告
     */
    @Override
    public Boolean updateByBo(ReportSnhntKwlqdbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        ReportSnhntKwlqdbgBsInfo update = BeanUtil.toBean(bo, ReportSnhntKwlqdbgBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    private void fillExcelResult(ReportSnhntKwlqdbgBsInfoBo bo) {
//        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
//            for (TgsybgBaDetailVo detailVo : bo.getBaDetailVos()) {
//                if ("不符合".equals(detailVo.getJiegpd())) {
//                    bo.setExcelResult(2);
//                    return;
//                }
//            }
//            bo.setExcelResult(1);
//        }
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntKwlqdbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥混凝土抗弯拉强度试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
